#!/bin/sh
apt-get update 
apt-get upgrade -y

#install server utils
apt-get install -y whois
apt-get install -y traceroute
apt-get install -y openssh
apt-get install -y nload

#build environment
apt-get install -y apache2
apt-get install -y subversion
apt-get install -y mysql-server
apt-get install -y php5
apt-get install -y php5-mysql
apt-get install -y php5-curl
apt-get install -y php5-mcrypt
apt-get install -y php5-gd

sed -i 's/;error_log = php_errors.log/error_log = \/var\/log\/php_errors.log/g' /etc/php5/cli/php.ini
sed -i 's/;error_log = php_errors.log/error_log = \/var\/log\/php_errors.log/g' /etc/php5/apache2/php.ini

a2enmod php5 
a2dissite default
apachectl restart

#install the server agent to this server
svn checkout https://openbiz-appcloud-cluster.googlecode.com/svn/trunk/server_agent /usr/local/appcloud_server_agent/

#install appcloud instance repos
svn checkout http://svn.openbiz.cn/appcloud /usr/local/appcloud_server_agent/repos/appcloud

#install cronjobs
echo "*/1 * * * * /usr/local/appcloud_server_agent/bin/agent.php" >> /var/spool/cron/crontabs/root
echo "*/1 * * * * /usr/bin/svn up /usr/local/appcloud_server_agent/" >> /var/spool/cron/crontabs/root
#echo "*/1 * * * * /usr/bin/svn up /usr/local/appcloud_server_agent/repos/appcloud" >> /var/spool/cron/crontabs/root
echo "1   0 * * * /usr/sbin/ntpdate asia.pool.ntp.org" >> /var/spool/cron/crontabs/root
chmod 700 /var/spool/cron/crontabs/root
chown root:crontab /var/spool/cron/crontabs/root
service cron restart

#create a new default site
mkdir -p /var/www/default/
mkdir -p /var/www/default/logs/
svn checkout https://openbiz-appcloud-cluster.googlecode.com/svn/trunk/server_default_site /var/www/default/httpdocs
chown -R www-data:www-data /var/www/default/
chmod -R 755 /var/www/default/
echo "# Appcloud Server Default Site
<VirtualHost *:80>
        ServerAdmin support@openbiz.me
        ServerName servercluster.me
        DocumentRoot /var/www/default/httpdocs
        ErrorLog /var/www/default/logs/error.log
        CustomLog /var/www/default/logs/access.log combined
        <Directory /var/www/default/httpdocs >
                Options -Indexes
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
        <IfModule mod_php5.c>
                php_admin_flag engine On
                php_admin_value open_basedir "/var/www/default/httpdocs:/tmp"
        </IfModule>                
</VirtualHost>
" > /etc/apache2/sites-available/000-server_default_site

a2ensite 000-server_default_site
apachectl restart
echo "default site created"

/usr/sbin/ntpdate asia.pool.ntp.org

ifconfig
vim /usr/local/appcloud_server_agent/config.php
